/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.suai.dao.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.suai.dao.Buffer;
import org.suai.dao.Package;

/**
 *
 * @author awm
 */
public class BufferAloha implements Buffer {
    private Map buffer = new HashMap();

    public void setPackage(int userNumber, Package pkg) {
        Integer index = new Integer(userNumber);
        buffer.put(index, pkg);
    }

    public Package getPackage(int userNumber) {
        return (Package)buffer.get( new Integer(userNumber) );
    }


    public List getPackagesForService() {
        List result = new LinkedList();

        for(Object key : buffer.keySet().toArray()) {
            Package item = (Package)buffer.get(key);
            if(item != null) {
                result.add(item);
            }
        }

        return result;
    }

    public void servicePackage(int userNumber) {
        Integer index = new Integer(userNumber);
        buffer.put(index, null);
    }

    @Override
    public boolean isFree(int userNumber) {
        Integer index = new Integer(userNumber);
        if( !buffer.containsKey(index) || buffer.get(index) == null ) {
            return true;
        }

        return false;
    }

}
